package com.hongkzh.www.shop.service;

import com.hongkzh.www.adv.service.BaseService;
import com.hongkzh.www.conmon.DateUtil;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

import java.sql.Time;
import java.util.*;

public class ShopService extends BaseService {

    public Record getShopById(String shopId, String loginUid) {
        Record record = Db.findFirst(Db.getSqlPara("shop.getShopInfoById", shopId));
        List<Record> list = Db.find(Db.getSqlPara("shop.getShopCarouselById", shopId));
        for (Record r : list
        ) {
            doImgPath(r);
        }
        doImgPath(record);
        record.set("carousels", list);

        record.set("isCollect", 0);
        if (StrKit.notBlank(loginUid)) {
            record.set("isCollect", Db.findFirst(Db.getSqlPara("shop.myShopcollect", shopId, loginUid)).getInt("collectCount"));
        }
        return record;
    }

    public Page<Record> getShopRemProduct(String shopId, int pageNumber) {
        Page<Record> page = Db.paginate(pageNumber, StaticPublic.LARGEPAGESIZE, Db.getSqlPara("shop.getShopRemProduct", shopId));
        for (Record r : page.getList()
        ) {
            doImgPath(r);
        }

        return page;
    }

    public Page<Record> getShopProduct(String shopId, int pageNumber) {
        Page<Record> page = Db.paginate(pageNumber, StaticPublic.LARGEPAGESIZE, Db.getSqlPara("shop.getShopProduct", shopId));
        for (Record r : page.getList()
        ) {
            doImgPath(r);
        }

        return page;
    }

    public Page<Record> getShopProductByOrder(String shopId, int pageNumber) {
        Page<Record> page = Db.paginate(pageNumber, StaticPublic.LARGEPAGESIZE, Db.getSqlPara("shop.getShopProductByOrder", shopId));
        for (Record r : page.getList()
        ) {
            doImgPath(r);
        }

        return page;
    }

    public Page<Record> getShopProductByCreate(String shopId, int pageNumber) {
        Page<Record> page = Db.paginate(pageNumber, StaticPublic.LARGEPAGESIZE, Db.getSqlPara("shop.getShopProductByCreateGroup", shopId));
        for (Record r : page.getList()
        ) {
            List<Record> products = Db.find(Db.getSqlPara("shop.getShopProductByCreate", shopId, r.getStr("day1")));
            for (Record r1 : products
            ) {
                doImgPath(r1);
            }
            r.set("products", products);
        }

        return page;
    }

    public void collectShopById(String shopId, String loginUid, String state) {
        if (state.equals("1")) {
            Db.update(Db.getSqlPara("shop.saveMyShopcollect", IdGen.uuid(), shopId, loginUid));

        } else {
            Db.update(Db.getSqlPara("shop.deleteMyShopcollect", shopId, loginUid));
        }
    }


    public Record getShopInfo(String shopId) {
        Record r = Db.findFirst(Db.getSqlPara("shop.getShopInfo", shopId));
        //products 商品数 news 上新数 hots 热销数 orders销量数

        return r;
    }

    public Record getNewUserVip() {
        Record record = new Record();
        SqlPara sqlPara = Db.getSqlPara("shop.getNewUserVip");
        List<Record> list = Db.find(sqlPara);
        int currentHour, beginDate, endDate;
        for (Record r : list) {
            currentHour = r.getInt("currentHour");
            beginDate = r.getInt("beginDate");
            endDate = r.getInt("endDate");
            r.set("sortDate", currentHour - beginDate);
            if (currentHour >= beginDate && currentHour < endDate) {
                r.set("sortDate", 0);
            } else {
                if (currentHour - beginDate > 0) {
                    r.set("sortDate", currentHour - beginDate - 24);
                }
            }
        }
        Collections.sort(list, new Comparator<Record>() {
            //升序排序
            @Override
            public int compare(Record o1,
                               Record o2) {
                return o2.getInt("sortDate").compareTo(o1.getInt("sortDate"));
            }

        });
        record.set("types", list);
        return record;
    }

    public Record getLuckyBurst() {
        Record record = new Record();
        SqlPara sqlPara = Db.getSqlPara("shop.getLuckyBurst");
        List<Record> list = Db.find(sqlPara);
        int currentHour, beginDate, endDate;
        Time currentTime;
        Time beginDate1;
        Time endDate1;
        for (Record r : list) {
            currentHour = r.getInt("currentHour");
            beginDate = r.getInt("beginDate");
            endDate = r.getInt("endDate");

            currentTime = r.getTime("currentTime");
            beginDate1 = r.getTime("beginDate1");
            endDate1 = r.getTime("endDate1");
//            r.set("sortDate", endDate1.getTime() - currentTime.getTime());
            r.set("sortDate", endDate1.getTime() - currentTime.getTime());
            if (currentTime.before(endDate1) && currentTime.after(beginDate1)) {
                r.set("sortDate", 0);
            }else{
                r.set("sortDate",-1);
//                if (currentTime)
            }
        }

        record.set("types", list);
        return record;
    }

    public Page<Record> getNewUserVipList(String id, int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("shop.getNewUserVipList", id);
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.LARGEPAGESIZE, sqlPara);
        for (Record r : records.getList()) {
            doImgPath(r);
        }
        return records;
    }


    public Page<Record> getCollageList(String sortId, String sortValue, int pageNumber) {
        Kv cond = Kv.by("isNew", "1").set("order", " order by " + sortId + " " + sortValue);
        SqlPara sqlPara = Db.getSqlPara("shop.getCollageList", cond);
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.LARGEPAGESIZE, sqlPara);
        for (Record r : records.getList()) {
            doImgPath(r);
        }
        return records;
    }

    public Record getLuckyBurstList(String id, String loginUid) {
        Record r = Db.findFirst(Db.getSqlPara("shop.getLuckyBurstList", id));
        if (null == r) {
            return r;
        }
        doImgPath(r);
        r.set("u", null);
        if (StrKit.notBlank(loginUid)) {
            Record r3 = Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserRanking", r.getStr("burstCouponId"), loginUid));
            doImgPath(r3);
            r.set("u", r3);
        }
        List<Record> list = Db.find(Db.getSqlPara("shop.getLuckyBurstUserRankingList", r.getStr("burstCouponId")));
        r.set("list", null);
        if (null != list && list.size() > 0) {
            for (int i = 0, s = list.size(); i < s; i++) {
                if (i > 0 && list.get(i).getInt("ranking").equals(list.get(i - 1).getInt("ranking"))) {
                    list.get(i).set("ranking", list.get(i - 1).getInt("ranking"));
                } else {
                    list.get(i).set("ranking", (i + 1));
                }
                doImgPath(list.get(i));
            }
            r.set("list", list);
        }
        for (int i = 0, s = list.size(); i < s; i++) {
            if (r.get("u") != null) {
                Record r3 = r.get("u");
                if (r3.getStr("id").equals(list.get(i).get("id"))) {
                    r3.set("ranking", list.get(i).getInt("ranking"));
                    r.set("u", r3);
                }
            }
        }
        int currentHour, beginDate, endDate;

        currentHour = r.getInt("currentHour");
        beginDate = r.getInt("beginDate");
        endDate = r.getInt("endDate");
        r.set("sortDate", endDate - currentHour);
        if (currentHour >= beginDate && currentHour < endDate) {
            r.set("sortDate", 0);
        }

        r.set("userCount", Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserCount", r.getStr("burstCouponId"))).getInt("userCount"));
        return r;
    }


    public List<Record> getLuckyBurstListEx(String id, String loginUid) {

//        LinkedList<Record> returnList =new LinkedList<>();

        Record luckyBurstTypeRecord= Db.findFirst(Db.getSqlPara("shop.getLuckyBurstTypeById", id));

            if (luckyBurstTypeRecord==null) {
            return null;
        }
        int currentHour, beginDate, endDate;
        currentHour = luckyBurstTypeRecord.getInt("currentHour");
        beginDate = luckyBurstTypeRecord.getInt("beginDate");
        endDate = luckyBurstTypeRecord.getInt("endDate");

        Date beginTime = luckyBurstTypeRecord.getTime("beginDate1");
        Date endTime = luckyBurstTypeRecord.getTime("endDate1");

        Kv cond= new Kv().set("del","0").set("burst","and a.play_time   BETWEEN "+ " '"+beginTime + "' " + "and " + " '"+ endTime+"'");

        List<Record> recordList =Db.find(Db.getSqlPara("shop.getLuckyBurstListEx1",cond));
        for (Record r : recordList) {



        }


        for (Record r : recordList)
        {

            doImgPath(r);
            r.set("userBuyState", 0);

            if (StrKit.notBlank(loginUid)) {
                Record r3 = Db.findFirst(Db.getSqlPara("shop.getLuckyBurstId", r.getStr("burstCouponId"), loginUid));
                if (r3!=null){
                    r.set("orderNumber", r3.getStr("orderNumber"));
                    String userState=r3.getStr("state");
                    if (StrKit.notBlank(userState)){
                        if ("-1".equals(userState)){
                            r.set("userBuyState", 1);
                        }else{
                            r.set("userBuyState", 2);
                        }
                    }
                }
            }
            r.set("u", null);
            if (StrKit.notBlank(loginUid)) {
                Record r3 = Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserRanking", r.getStr("burstCouponId"), loginUid));
                doImgPath(r3);
                r.set("u", r3);
            }
            r.set("beginTime", beginTime);
            r.set("endTime", endTime);

            r.set("sortDate", 1);
            if (currentHour >= beginDate && currentHour < endDate) {
                //活动中
                r.set("sortDate", 0);
            }else{
                if (currentHour < beginDate) {
                    //活动未开始
                    r.set("sortDate", -1);
                }
                else{
                    r.set("sortDate", 1);
                }

            }

        }
        return recordList;
    }

    public Page<Record> getLuckyBurstFriends(String orderNumber, int pageNumber) {
        Page<Record> recordPage = Db.paginate(pageNumber, StaticPublic.LARGEPAGESIZE, Db.getSqlPara("shop.getLuckyBurstFriends", orderNumber));
        for (Record r : recordPage.getList()
        ) {
            doImgPath(r);
            r.set("createDate", DateUtil.CalculateTime(r.getStr("createDate")));
        }

        return recordPage;
    }
}
